package top.meeting.room.module.system.controllers;

import com.jfinal.aop.Before;
import com.jfinal.ext.interceptor.GET;
import com.jfinal.ext.interceptor.POST;
import com.jfinal.kit.StrKit;
import com.jfinal.log.Log;
import com.jfinal.log.Log4jLog;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;
import top.jfinal.jwtTokenPlugin.annotation.Auth;
import top.meeting.room.common.base.BaseController;
import top.meeting.room.module.system.form.ForceForm;
import top.meeting.room.module.system.form.ForceMenuForm;
import top.meeting.room.module.system.form.RootMenuForm;
import top.meeting.room.module.system.form.SonMenuForm;
import top.meeting.room.module.system.model.Force;
import top.meeting.room.module.system.query.ForceQuery;
import top.meeting.room.module.system.services.impl.ForceService;

import java.util.Arrays;
import java.util.List;

/**
 * 我有故事，你有酒么？
 * JKhaled created by yunqisong@foxmail.com 2017/7/20
 * FOR : 权限API控制
 */
@Auth(hasForces = {"权限管理", "所有权限"})
public class ForceController extends BaseController {

    private static final Log _log = Log4jLog.getLog(ForceController.class);

    /**
     * 保存
     */
    @Before({POST.class, Tx.class})
    public void save() {
        ForceForm forceForm = getJsonParaToObj(ForceForm.class);
        _log.debug("Save force by forceForm : {}" + String.valueOf(forceForm));
        Record force = ForceService.me.saveAsRecord(forceForm);
        if (force != null)
            renderJsonSuccess(force);
        else
            renderJsonError("保存权限异常");
    }

    /**
     * 删除
     */
    @Before({GET.class, Tx.class})
    public void delete() {
        Long id = getParaToLong();
        _log.debug("Delete force by id : {}" + String.valueOf(id));
        if (id != null) {
            ForceService.me.deleteById(id);
            renderJsonSuccess();
        } else
            renderJsonError("删除的ID不可以为空");
    }

    /**
     * 集体删除
     */
    @Before({GET.class, Tx.class})
    public void deletes() {
        String ids = getPara();
        _log.debug("Delete force by ids : {}" + String.valueOf(ids));
        if (StrKit.notBlank(ids)) {
            ForceService.me.deleteByIds(ids);
            renderJsonSuccess();
        } else
            renderJsonError("删除的ID不可以全为空");
    }

    /**
     * 查询 列表
     */
    @Before({GET.class})
    public void find() {
        ForceQuery forceQuery = getUrlParaToObj(ForceQuery.class);
        _log.debug("Find page<force> by forceQuery : {}" + String.valueOf(forceQuery));
        Page<Record> retVal = ForceService.me.pageRecord(forceQuery);
        if (retVal != null)
            renderJsonSuccess(retVal);
        else
            renderJsonSuccess(Arrays.asList());
    }

    /**
     * 权限树
     */
    @Before({GET.class})
    @Auth(hasForces = {"角色管理"})
    public void tree() {
        _log.debug("Tree force : {}");
        List<Record> retVal = ForceService.me.tree();
        if (retVal != null)
            renderJsonSuccess(retVal);
        else
            renderJsonSuccess(Arrays.asList());
    }

    /**
     * 查询 个体
     */
    @Before({GET.class})
    public void get() {
        Long id = getParaToLong();
        _log.debug("Get force by id : {}" + String.valueOf(id));
        if (id != null)
            renderJsonSuccess(ForceService.me.getRecord(id));
        else
            renderJsonError("查询的ID不可以为空");
    }

    /**
     * 保存根菜单
     */
    @Before({Tx.class})
    public void saveRootMenu() {
        RootMenuForm rootMenuForm = getJsonParaToObj(RootMenuForm.class);
        _log.debug("Save force by rootMenuForm : {}" + String.valueOf(rootMenuForm));
        Force force = ForceService.me.saveRootMenu(rootMenuForm);
        if (force != null)
            renderJsonSuccess(force.toBack());
        else
            renderJsonError("保存权限异常");
    }

    /**
     * 保存子菜单
     */
    @Before({Tx.class})
    public void saveSonMenu() {
        SonMenuForm sonMenuForm = getJsonParaToObj(SonMenuForm.class);
        _log.debug("Save force by sonMenuForm : {}" + String.valueOf(sonMenuForm));
        Force force = ForceService.me.saveSonMenu(sonMenuForm);
        if (force != null)
            renderJsonSuccess(force.toBack());
        else
            renderJsonError("保存权限异常");

    }

    /**
     * 保存权限
     */
    @Before({Tx.class})
    public void saveForceMenu() {
        ForceMenuForm forceMenuForm = getJsonParaToObj(ForceMenuForm.class);
        _log.debug("Save force by forceMenuForm : {}" + String.valueOf(forceMenuForm));
        Force force = ForceService.me.saveForceMenu(forceMenuForm);
        if (force != null)
            renderJsonSuccess(force.toBack());
        else
            renderJsonError("保存权限异常");

    }
}
